#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<string>
#include<ctime>

using namespace std;

void* thread_run(void* args)
{
    const char* name = static_cast<const char*>(args);

    int cnt=5;
    while(cnt)
    {
        cout<<name<<"is running"<<cnt--<<"获取该线程id"<<pthread_self()<<endl;
        sleep(1);
    }
    //5s后就终止线程
    pthread_exit((void*)11);
}

int main()
{
    pthread_t tid;
    //创建单进程单线程
    pthread_create(&tid,nullptr,thread_run,(void*)"thread1");

    sleep(3);//睡眠3s就取消线程

    pthread_cancel(tid);

    void* ret=nullptr;
    //等待线程
    pthread_join(tid,&ret);
    cout<<"new thread exit:"<<(int64_t)ret<<"退出线程"<<tid<<endl;
    return 0;
}
